package de.ohmhochschule.bme;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class PaintView extends View {
	private final FileIOActivity paintActvty;

	public PaintView(Context context) {
		super(context);
		this.paintActvty = (FileIOActivity) context;
		setFocusable(true);
		setFocusableInTouchMode(true);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		setBackgroundColor(Color.WHITE);

		// draw path:
		Paint gridColor = new Paint();
		gridColor.setStrokeWidth(2);
		gridColor.setColor(Color.BLACK);
		ArrayList<Float> points = paintActvty.getPoints();
		if ( points.size() > 3 ) {
			float xOld = points.get(0);
			float yOld = points.get(1);
			for (int i = 2; i < points.size(); i+=2) {
				float x = points.get(i);
				float y = points.get(i+1);
				canvas.drawLine(xOld, yOld, x, y, gridColor);
				xOld = x;
				yOld = y;
			}			
		}
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() != MotionEvent.ACTION_DOWN) {
			return super.onTouchEvent(event);
		}
		paintActvty.addLineToPath(event.getX(), event.getY());
		return true;
	}

}
